/**
 * Copyright (c) 2008 ubermath
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'ubermath' nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Created: Aug 26, 2008
 */
package com.googlecode.ubermath;

import java.util.Arrays;

/**
 * @author Rebecca Hicks
 */
public class MatrixOperations {

	// Swaps row a with row b
	public static final void swapRow(float[][] matrix, int a, int b) {
		float[] temp = matrix[a];
		matrix[a] = matrix[b];
		matrix[b] = temp;
	}
	
	public static final void swapRow(int[][] matrix, int a, int b) {
		int[] temp = matrix[a];
		matrix[a] = matrix[b];
		matrix[b] = temp;
	}
	
	// Divides row a by matrix[a][b]
	public static final void divideRow(float[][] matrix, int a, int b) {
		float temp = matrix[a][b];
		for (int i = 0; i < matrix[a].length; i++) {
			matrix[a][i] /= temp;
		}
	}

	// Eliminate all rows with a nonzero value in column b, using row a
	public static final void eliminate(float[][] matrix, int a, int b) {
		for (int i = 0; i < matrix.length; i++) {
			if (i != a) {
				if (matrix[i][b] != 0) {
					float temp = -1 * (matrix[i][b]);
					for (int j = b; j < matrix[i].length; j++) {
						matrix[i][j] += (temp * matrix[a][j]);
					}
				}
			}
		}
	}

	// Finds first row >= a with nonzero value in column b
	public static final int findNonzero(float[][] matrix, int a, int b) {
		for (int i = a; i < matrix.length; i++) {
			if (matrix[i][b] != 0) {
				return i;
			}
		}
		return -1;
	}
	
	public static final int findNonzero(int[][] matrix, int a, int b) {
		for (int i = a; i < matrix.length; i++) {
			if (matrix[i][b] != 0) {
				return i;
			}
		}
		return -1;
	}
	
	// Finds first row >= a with nonzero value in column b, rounding values closer to zero than a factor of r
	public static final int findNonzero(float[][] matrix, int a, int b, float r) {
		float round = 1.0f;
   		if (r > 0) {
    		for (int i = 1; i <= r; i++) {
    			round *= 10;
    		}
    	} else if (r < 0) {
    		r = Math.abs(r);
    		for (int i = 1; i <=r; i++) {
    			round /=10;
    		}
    	}
		
		for (int i = a; i < matrix.length; i++) {
			if (Math.round(matrix[i][b] * r) == 0) {
				matrix[i][b] = 0;
			}
			if (matrix[i][b] != 0) {
				return i;
			}
		}
		return -1;
	}
	
	//Sorts matrix by index a, sorted highest to lowest
	public static final void sortMatrix(float[][] matrix, int a) {
		for (int i = 0; i < matrix.length - 1; i++) {
			for (int j = i + 1; j < matrix.length; j++) {
				if (matrix[j][a] > matrix[i][a]) {
					swapRow(matrix, i, j);
				}
			}
		}
	}
	
	public static final void sortMatrix(int[][] matrix, int a) {
		for (int i = 0; i < matrix.length - 1; i++) {
			for (int j = i + 1; j < matrix.length; j++) {
				if (matrix[j][a] > matrix[i][a]) {
					swapRow(matrix, i, j);
				}
			}
		}
	}
	
	//Sorts matrix by index a * index b, sorted highest to lowest (weight given to a over b where product is equal)
	public static final void sortMatrix(float[][] matrix, int a, int b) {
		for (int i = 0; i < matrix.length - 1; i++) {
			for (int j = i + 1; j < matrix.length; j++) {
				if (matrix[j][a] * matrix[j][b] == matrix[i][a] * matrix[i][b]) {
					if (matrix[j][a] > matrix[i][a]) {
						swapRow(matrix, i, j);
					}
				} else if (matrix[j][a] * matrix[j][b] > matrix[i][a] * matrix[i][b]) {
					swapRow(matrix, i, j);
				}
			}
		}
	}
	
	public static final void sortMatrix(int[][] matrix, int a, int b) {
		for (int i = 0; i < matrix.length - 1; i++) {
			for (int j = i + 1; j < matrix.length; j++) {
				if (matrix[j][a] * matrix[j][b] == matrix[i][a] * matrix[i][b]) {
					if (matrix[j][a] > matrix[i][a]) {
						swapRow(matrix, i, j);
					}
				} else if (matrix[j][a] * matrix[j][b] > matrix[i][a] * matrix[i][b]) {
					swapRow(matrix, i, j);
				}
			}
		}
	}

	//Finds the identity matrix of the given matrix
	public static final float[][] identityMatrix(float[][] matrix) {
		float[][] identity = Arrays.copyOf(matrix, matrix.length);
		int i = 0;
		for (int row = 0; row < matrix.length; row++) {
			for (int col = i; col < (matrix[row].length); col++) {
				int temp = findNonzero(matrix, row, col);
				if (temp == -1) {
					i++;
				} else {
					if (row != temp) {
						swapRow(matrix, row, temp);
					}
					if (matrix[row][col] != 1) {
						divideRow(matrix, row, col);
					}
					eliminate(matrix, row, col);
					i++;
					break;
				}
			}
		}
		return identity;
	}
	
	public static String toString(float[][] equation) {
		if (equation == null) {
			return null;
		}
		
		StringBuilder b = new StringBuilder(); {
			for (float[] row : equation) {
				b.append('[');
				boolean first = true;
				for (float column : row) {
					if (!first) {
						b.append(", ");
					} else {
						first = false;
					}
					b.append(column);
				}
				b.append(']');
				b.append('\r');
				b.append('\n');
			}
		}
		
		return b.toString();
	}
	
	public static String toString(int[][] equation) {
		if (equation == null) {
			return null;
		}
		
		StringBuilder b = new StringBuilder(); {
			for (int[] row : equation) {
				b.append('[');
				boolean first = true;
				for (int column : row) {
					if (!first) {
						b.append(", ");
					} else {
						first = false;
					}
					b.append(column);
				}
				b.append(']');
				b.append('\r');
				b.append('\n');
			}
		}
		
		return b.toString();
	}
}
